<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use Exception;
use think\Request;
use think\Response;

class Dictionary extends BaseController
{
    protected function model(): \app\model\Dictionary
    {
        return new \app\model\Dictionary();
    }

    /**
     * 获取状态
     * @return Response
     */
    public function index(): Response
    {
        try {
            $oid = $this->organization;
            $result = $this->model()->where('oid', $oid)
                ->order('sort', 'desc')
                ->order('id', 'asc')->select()->toArray();
            return apiResponse(200, 'success', $result);
        } catch (Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 保存新建的资源
     *
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        try {
            $data = $request->param();
            $oid = $this->organization;
            $data['oid'] = $oid;
            $status = $this->model()->where('oid', $oid)
                ->where('type', $data['type'])
                ->where('name', $data['name'])
                ->findOrEmpty();
            if (!$status->isEmpty()) {
                return apiResponse(400, '名称已存在，无法重复添加');
            }
            $this->model()->save($data);
            return apiResponse(200, '添加成功');
        } catch (Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 保存更新的资源
     * @param Request $request
     * @param int $id
     * @return Response
     */
    public function update(Request $request, int $id): Response
    {
        try {
            $data = $request->param();
            $oid = $this->organization;
            $status = $this->model()->where('oid', $oid)
                ->where('id', '<>', $id)
                ->where('type', $data['type'])
                ->where('name', $data['name'])
                ->findOrEmpty();
            if (!$status->isEmpty()) {
                return apiResponse(400, '名称已存在，无法重复添加');
            }
            $this->model()->where('id', $id)->update($data);
            return apiResponse(200, '更新成功');
        }catch (Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }
    //批量更新
    public function batch_status(Request $request): Response
    {
        try {
            $data = $request->param();
            $oid = $this->organization;
            $this->model()->where('oid', $oid)
                ->whereIn('id', $data['ids'])
                ->update($data['data']);
            return apiResponse(200, '更新成功');
        } catch (Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return Response
     */
    public function delete(int $id): Response
    {
        try {
            $oid = $this->organization;
            $this->model()->where('oid', $oid)
                ->where('id', $id)
                ->delete();
            return apiResponse(200, '删除成功');
        } catch (Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }
    //批量删除
    public function batch_delete(Request $request): Response
    {
        try {
            $ids = $request->param('ids');
            $oid = $this->organization;
            $this->model()->where('oid', $oid)
                ->whereIn('id', $ids)
                ->delete();
            return apiResponse(200, '删除成功');
        } catch (Exception $e){
            return apiResponse($e->getCode(), $e->getMessage());
        }
    }
}
